﻿1./*  
2. * 这个脚本用于保持页面跳转前后滚动条的位置一致  
3. * bnc.badboy  
4. * 2009-8-24  
5. */  
6.  
7.function ScrollHolder()   
8.{   
9.    //add event to window   
10.    this.addWindowListener = function(sEventType, fnHandler) {   
11.        if (window.addEventListener) {   
12.            window.addEventListener(sEventType, fnHandler, false);   
13.        } else if (window.attachEvent) {   
14.            window.attachEvent("on" + sEventType, fnHandler);   
15.        } else {   
16.            window["on" + sEventType] = fnHandler;   
17.        }   
18.    }   
19.       
20.    //remember scrolling information when window is unloading   
21.    this.whenUnload = function()   
22.    {   
23.        var scrollTop = document.documentElement.scrollTop;   
24.        document.cookie = "scrollTop="+scrollTop;   
25.    }   
26.       
27.    //set current scroll bar the last page scroll bar position   
28.    this.whenLoad = function()   
29.    {   
30.        var scrollTop = document.cookie.match(new RegExp("(^| )scrollTop=([^;]*)(;|$)"));   
31.        if(scrollTop==null)   
32.            scrollTop = 0;   
33.        window.scrollTo(0,scrollTop[2]);   
34.    }   
35.       
36.    //run this script   
37.    this.run = function()   
38.    {   
39.        this.addWindowListener("unload", this.whenUnload);   
40.        this.addWindowListener("load", this.whenLoad);   
41.    }   
42.}   
43.  
44.var scrollHolder = new ScrollHolder();   
45.scrollHolder.run();  
